#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QColorDialog"
#include "qcursor.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    drawView = ui->graphicsView;
    statusBar = ui->statusBar;

    actionList.append(ui->action_choose);

    actionList.append(ui->action_draw);
    actionList.append(ui->action_line);
    actionList.append(ui->action_curve);
    actionList.append(ui->action_rect);
    actionList.append(ui->action_round_rect);
    actionList.append(ui->action_circle);
    actionList.append(ui->action_ellipse);
    actionList.append(ui->action_undo);
    actionList.append(ui->action_redo);
    ui->action_draw->toggle();

    ui->action1->setChecked(true);  //ui上面：画笔大小默认为1
    drawView->setSize(this->width(), this->height());
    for(int i = 0; i < this->width(); i++){
        for(int j = 0; j < this->height(); j++){
            drawView->setRecordColor(Qt::white, i, j);
        }
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::toggleTool(QAction *a){
    foreach (QAction * action, actionList){
        if (action != a){
            action->setChecked(false);
            action->toggled(false);
        }
    }
}

void MainWindow::onToggleFalse()
{
    drawView->setMode(empty);
    statusBar->showMessage("请选择涂鸦工具");
}

void MainWindow::on_action_del_triggered()
{
    //paintWidget->clear();

    //默认情况下删除整张画布
    drawView->clear();

}

void MainWindow::on_action_redo_triggered()
{
    drawView->redo();
}

void MainWindow::on_action_undo_triggered()
{
    drawView->undo();
}

void MainWindow::on_action_draw_toggled(bool arg1)
{
    if (arg1){
        toggleTool(ui->action_draw);
        drawView->setMode(draw);
        drawView->setWidth(weight);
        drawView->setColor(color);
        drawView->setStyle(Qt::SolidLine);
        statusBar->showMessage("就绪-画笔");
    }
    else {
        onToggleFalse();
    }
}

void MainWindow::on_action_line_toggled(bool arg1)
{
    if (arg1){
        toggleTool(ui->action_line);
        drawView->setMode(line);
        drawView->setWidth(weight);
        drawView->setColor(color);
        drawView->setStyle(Qt::SolidLine);
        statusBar->showMessage("就绪-直线");
    }
    else {
        onToggleFalse();
    }
}

void MainWindow::on_action_curve_toggled(bool arg1)
{
    if (arg1){
        toggleTool(ui->action_curve);
        drawView->setMode(curve);
        drawView->setWidth(weight);
        drawView->setColor(color);
        drawView->setStyle(Qt::SolidLine);
        statusBar->showMessage("就绪-曲线");
    }
    else {
        onToggleFalse();
    }
}

void MainWindow::on_action_rect_toggled(bool arg1)
{
    if (arg1){
        toggleTool(ui->action_rect);
        drawView->setMode(rectangle);
        drawView->setWidth(weight);
        drawView->setColor(color);
        drawView->setBColor(b_color);
        drawView->setStyle(Qt::SolidLine);
        statusBar->showMessage("就绪-矩形");
    }
    else {
        onToggleFalse();
    }
}

void MainWindow::on_action_round_rect_toggled(bool arg1)
{
    if (arg1){
        toggleTool(ui->action_round_rect);
        drawView->setMode(roundrect);
        drawView->setWidth(weight);
        drawView->setColor(color);
        drawView->setBColor(b_color);
        drawView->setStyle(Qt::SolidLine);
        statusBar->showMessage("就绪-圆角矩形");
    }
    else {
        onToggleFalse();
    }
}

void MainWindow::on_action_circle_toggled(bool arg1)
{
    if (arg1){
        toggleTool(ui->action_circle);
        drawView->setMode(circle);
        drawView->setWidth(weight);
        drawView->setColor(color);
        drawView->setBColor(b_color);
        drawView->setStyle(Qt::SolidLine);
        statusBar->showMessage("就绪-圆形");
    }
    else {
        onToggleFalse();
    }
}

void MainWindow::on_action_ellipse_toggled(bool arg1)
{
    if (arg1){
        toggleTool(ui->action_ellipse);
        drawView->setMode(ellipse);
        drawView->setWidth(weight);
        drawView->setColor(color);
        drawView->setBColor(b_color);
        drawView->setStyle(Qt::SolidLine);
        statusBar->showMessage("就绪-椭圆");
    }
    else {
        onToggleFalse();
    }
}

void MainWindow::on_action_choose_toggled(bool arg1)
{
    if (arg1){
        toggleTool(ui->action_choose);
        drawView->setMode(choose);

        statusBar->showMessage("就绪-选择工具");
    }
    else {
        onToggleFalse();
    }
}


void MainWindow::on_action_addColor_toggled(bool arg1)
{
    drawView->setAddColorTag(arg1);
    if (arg1){
        toggleTool(ui->action_addColor);
        statusBar->showMessage("就绪-填充");
    }
    else {
        onToggleFalse();
    }
}

/*
    tqn
    设置画笔大小的单选功能。
*/

void MainWindow::on_action1_triggered()
{
    weight = 1;
    drawView->setWidth(weight);
    ui->action1->setChecked(true);
    ui->action2->setChecked(false);
    ui->action3->setChecked(false);
    ui->action4->setChecked(false);
    ui->action5->setChecked(false);
    ui->action6->setChecked(false);
    ui->action7->setChecked(false);
    ui->action8->setChecked(false);
    ui->action9->setChecked(false);
    ui->action10->setChecked(false);
    statusBar->showMessage("就绪-选择画笔大小1");
}

void MainWindow::on_action2_triggered()
{
    weight = 2;
    drawView->setWidth(weight);
    ui->action1->setChecked(false);
    ui->action2->setChecked(true);
    ui->action3->setChecked(false);
    ui->action4->setChecked(false);
    ui->action5->setChecked(false);
    ui->action6->setChecked(false);
    ui->action7->setChecked(false);
    ui->action8->setChecked(false);
    ui->action9->setChecked(false);
    ui->action10->setChecked(false);
    statusBar->showMessage("就绪-选择画笔大小2");
}

void MainWindow::on_action3_triggered()
{
    weight = 3;
    drawView->setWidth(weight);
    ui->action1->setChecked(false);
    ui->action2->setChecked(false);
    ui->action3->setChecked(true);
    ui->action4->setChecked(false);
    ui->action5->setChecked(false);
    ui->action6->setChecked(false);
    ui->action7->setChecked(false);
    ui->action8->setChecked(false);
    ui->action9->setChecked(false);
    ui->action10->setChecked(false);
    statusBar->showMessage("就绪-选择画笔大小3");
}

void MainWindow::on_action4_triggered()
{
    weight = 4;
    drawView->setWidth(weight);
    ui->action1->setChecked(false);
    ui->action2->setChecked(false);
    ui->action3->setChecked(false);
    ui->action4->setChecked(true);
    ui->action5->setChecked(false);
    ui->action6->setChecked(false);
    ui->action7->setChecked(false);
    ui->action8->setChecked(false);
    ui->action9->setChecked(false);
    ui->action10->setChecked(false);
    statusBar->showMessage("就绪-选择画笔大小4");
}

void MainWindow::on_action5_triggered()
{
    weight = 5;
    drawView->setWidth(weight);
    ui->action1->setChecked(false);
    ui->action2->setChecked(false);
    ui->action3->setChecked(false);
    ui->action4->setChecked(false);
    ui->action5->setChecked(true);
    ui->action6->setChecked(false);
    ui->action7->setChecked(false);
    ui->action8->setChecked(false);
    ui->action9->setChecked(false);
    ui->action10->setChecked(false);
    statusBar->showMessage("就绪-选择画笔大小5");
}

void MainWindow::on_action6_triggered()
{
    weight = 6;
    drawView->setWidth(weight);
    ui->action1->setChecked(false);
    ui->action2->setChecked(false);
    ui->action3->setChecked(false);
    ui->action4->setChecked(false);
    ui->action5->setChecked(false);
    ui->action6->setChecked(true);
    ui->action7->setChecked(false);
    ui->action8->setChecked(false);
    ui->action9->setChecked(false);
    ui->action10->setChecked(false);
    statusBar->showMessage("就绪-选择画笔大小6");
}

void MainWindow::on_action7_triggered()
{
    weight = 7;
    drawView->setWidth(weight);
    ui->action1->setChecked(false);
    ui->action2->setChecked(false);
    ui->action3->setChecked(false);
    ui->action4->setChecked(false);
    ui->action5->setChecked(false);
    ui->action6->setChecked(false);
    ui->action7->setChecked(true);
    ui->action8->setChecked(false);
    ui->action9->setChecked(false);
    ui->action10->setChecked(false);
    statusBar->showMessage("就绪-选择画笔大小7");
}

void MainWindow::on_action8_triggered()
{
    weight = 8;
    drawView->setWidth(weight);
    ui->action1->setChecked(false);
    ui->action2->setChecked(false);
    ui->action3->setChecked(false);
    ui->action4->setChecked(false);
    ui->action5->setChecked(false);
    ui->action6->setChecked(false);
    ui->action7->setChecked(false);
    ui->action8->setChecked(true);
    ui->action9->setChecked(false);
    ui->action10->setChecked(false);
    statusBar->showMessage("就绪-选择画笔大小8");
}

void MainWindow::on_action9_triggered()
{
    weight = 9;
    drawView->setWidth(weight);
    ui->action1->setChecked(false);
    ui->action2->setChecked(false);
    ui->action3->setChecked(false);
    ui->action4->setChecked(false);
    ui->action5->setChecked(false);
    ui->action6->setChecked(false);
    ui->action7->setChecked(false);
    ui->action8->setChecked(false);
    ui->action9->setChecked(true);
    ui->action10->setChecked(false);
    statusBar->showMessage("就绪-选择画笔大小9");
}

void MainWindow::on_action10_triggered()
{
    weight = 10;
    drawView->setWidth(weight);
    ui->action1->setChecked(false);
    ui->action2->setChecked(false);
    ui->action3->setChecked(false);
    ui->action4->setChecked(false);
    ui->action5->setChecked(false);
    ui->action6->setChecked(false);
    ui->action7->setChecked(false);
    ui->action8->setChecked(false);
    ui->action9->setChecked(false);
    ui->action10->setChecked(true);
    statusBar->showMessage("就绪-选择画笔大小10");
}

void MainWindow::on_action_color_triggered()
{
    QColorDialog colorDialog;//调出颜色选择器对话框
    QColor c = colorDialog.getRgba();
    //c.red(), c.green(), c.blue()是分别对应的rgb值
    int r = c.red();
    int g = c.green();
    int b = c.blue();
    QString s = QString("background-color:rgb(%1, %2, %3)").arg(r).arg(g).arg(b);
    if(checkedId == 0)
    {
        color = c;
        drawView->setColor(c);
        ui->penColorBtn->setStyleSheet(s);
    } else if(checkedId == 1) {
        b_color = c;
        drawView->setBColor(c);
        ui->brushColorBtn->setStyleSheet(s);
    }
}

void MainWindow::on_action_rotate_triggered()
{
    toggleTool(nullptr);
    drawView->rotate(90);
    statusBar->showMessage("旋转成功");
}


void MainWindow::on_action_minus_triggered()
{
    toggleTool(nullptr);
    drawView->scale(1/1.2,1/1.2);
    statusBar->showMessage("缩小");
}

void MainWindow::on_action_plus_triggered()
{
    toggleTool(nullptr);
    drawView->scale(1.2,1.2);
    statusBar->showMessage("放大");
}
void MainWindow::on_action_img_triggered()
{
    toggleTool(nullptr);
    drawView->addImage();
}

void MainWindow::on_action_o_triggered()
{
    QMessageBox mb( "打开图片", "正在尝试打开图片，是否保存画布内容？", QMessageBox::Information, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No, QMessageBox::Cancel | QMessageBox::Escape );
    mb.setButtonText( QMessageBox::Yes, "保存" );
    mb.setButtonText( QMessageBox::No, "不保存" );
    mb.setButtonText( QMessageBox::Cancel, "取消" );
    switch(mb.exec()) {
        case QMessageBox::Yes:
            on_action_s_triggered();
            drawView->addImage();
            break;
        case QMessageBox::No:
            drawView->addImage();
            break;
        case QMessageBox::Cancel:
            break;
    }
}

void MainWindow::on_action_n_triggered()
{
    QMessageBox mb( "新建画布", "正在尝试新建画布，是否保存画布内容？", QMessageBox::Information, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No, QMessageBox::Cancel | QMessageBox::Escape );
    mb.setButtonText( QMessageBox::Yes, "保存" );
    mb.setButtonText( QMessageBox::No, "不保存" );
    mb.setButtonText( QMessageBox::Cancel, "取消" );
    switch(mb.exec()) {
        case QMessageBox::Yes:
            on_action_s_triggered();
            drawView->clear();
            break;
        case QMessageBox::No:
            drawView->clear();
            break;
        case QMessageBox::Cancel:
            break;
    }
}

void MainWindow::on_action_x_triggered()
{
    QMessageBox mb( "温馨提示", "画布内容未保存，是否保存画布内容？", QMessageBox::Information, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No, QMessageBox::Cancel | QMessageBox::Escape );
    mb.setButtonText( QMessageBox::Yes, "保存" );
    mb.setButtonText( QMessageBox::No, "不保存" );
    mb.setButtonText( QMessageBox::Cancel, "取消" );
    switch(mb.exec()) {
        case QMessageBox::Yes:
            on_action_s_triggered();
            this->close();
            break;
        case QMessageBox::No:
            this->close();
            break;
        case QMessageBox::Cancel:
            break;
    }
}

void MainWindow::on_action_s_triggered()
{
    drawView->saveImage();
}

void MainWindow::on_penColorBtn_clicked()
{
    //选中画笔颜色修改，则修改颜色的时候改变画笔颜色显示
    ui->penColorBtn->setChecked(true);
    ui->brushColorBtn->setChecked(false);
    checkedId = 0;
}

void MainWindow::on_brushColorBtn_clicked()
{
    ui->penColorBtn->setChecked(false);
    ui->brushColorBtn->setChecked(true);
    checkedId = 1;
}

void MainWindow::on_brushCheckBox_clicked()
{
    if(ui->brushCheckBox->isChecked() == true){
        drawView->setCheckBox(1);
    }else {
        drawView->setCheckBox(0);
    }
}
